package com.juzhencms.apps.busi.web;



import java.io.UnsupportedEncodingException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import javax.annotation.Resource;
import javax.imageio.ImageIO;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;


import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.alibaba.fastjson.JSON;
import com.juzhencms.apps.base.common.database.DBTable;
import com.juzhencms.apps.base.common.database.JdbcDao;
import com.juzhencms.apps.base.common.util.WebUtil;

import lombok.extern.slf4j.Slf4j;

@Controller
@RequestMapping("/config")
@Slf4j
public class ConfigController  extends BaseController{
	
	//批量修改保存网站配置
	@RequestMapping(value="/updateBatch",produces="text/html; charset=UTF-8")
	@ResponseBody
	public String updateBatch(HttpServletRequest request){

		String parameterValue = request.getParameter("data");
		Map data = JSON.parseObject(parameterValue, Map.class);
		Iterator entries = data.entrySet().iterator();
		while (entries.hasNext()) {
		    Map.Entry entry = (Map.Entry) entries.next();
		    
		    
		    
		    
		    String key = (String) entry.getKey(); 
		    String value = ""+entry.getValue();
		    
		    Map newData = new HashMap();
		    newData.put("name", key);
		    newData.put("content", value);
		    
		    
		    
		    this.getCommonService().getJdbcDao().updateObj(DBTable.config, newData, "name");
		}  
		
		Map result=new HashMap();
		result.put("result", 1);
		result.put("msg", "保存成功");
		
		return WebUtil.resultString(result,request);
		
			/*List fromJson = JSON.parseObject(parameterValue, List.class);
			for(int i = 0 ; i < fromJson.size() ; i++) {
				String json = JSON.toJSONString(fromJson.get(i));
				Map data = JSON.parseObject(json, Map.class);
				String id=null;
				if(data.get("id")!=null){
					id=data.get("id").toString();
				}
				if(id==null || id.equals("")){
				}else {
					this.getCommonService().getJdbcDao().updateObj(DBTable.config, data, "id");
				}
			}*/
			
			/*需要导jar 包
			//json对象 转为json数组  
			JSONArray array = JSONArray.fromObject(parameterValue);    //先读取字符串数组 
			  Object[]  objs = array.toArray();
			  
			  for (int i = 0; i < objs.length; i++) {
				String json = JSON.toJSONString(objs[i]);
				Map data = JSON.parseObject(json, Map.class); 
				String id=null;
				if(data.get("id")!=null){
					id=data.get("id").toString();
				}
				if(id==null || id.equals("")){
				}else {
					this.getCommonService().getJdbcDao().updateObj(DBTable.config, data, "id");
				}
			}*/
	
		
		
	}
	
	//保存一条网站配置
	@RequestMapping(value="/save",produces="text/html; charset=UTF-8")
	@ResponseBody
	public String save(HttpServletRequest request){
		
		
		
		Map data = JSON.parseObject(request.getParameter("data"), Map.class); 
		String id=null;
		if(data.get("id")!=null){
			id=data.get("id").toString();
		}
		
		if(id==null || id.equals("")){
			if(data.get("create_time")==null || data.get("create_time").equals("")){
				SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
				Date date = new Date();
				data.put("create_time", sdf.format(date));
				
			}
			this.getCommonService().getJdbcDao().addObj(DBTable.config, data);
		}else{
			
			this.getCommonService().getJdbcDao().updateObj(DBTable.config, data, "id");
		}
		
		Map result=new HashMap();
		result.put("result", 1);
		result.put("msg", "保存成功");
		
		return WebUtil.resultString(result,request);
		
	}
	
	
	
	
	
	//查询所有网站配置
	@RequestMapping(value="/gridlistData",produces="text/html; charset=UTF-8")
	@ResponseBody
	public String gridlistData(HttpServletRequest request){
		Map data=new HashMap();
		
		String where="where del_flag=0 and publish = 1 ";
		
		List jsonDataList=new ArrayList<>();
		Map jsonDataMap=new HashMap<>();
		//查询列表数据
		List dataList=this.getCommonService().getJdbcDao().querySql("select * from "+DBTable.config+" "+where);
		if(dataList!=null && dataList.size()>0){
			//SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
			for(int i=0;i<dataList.size();i++){
				Map row=(Map)dataList.get(i);
				//Date date = (Date)row.get("create_time");
				//row.put("create_time",sdf.format(date));
				jsonDataMap.put(""+row.get("name"), row.get("content"));
				//jsonDataList.add(jsonDataMap);
			}
			
		}
		
		//拼装返回的数据
		data.put("data", jsonDataMap);
		//data.put("itemsCount", recordsTotal);
		
		return WebUtil.resultString(data,request);
	}
	
	

	//用户修改
	@RequestMapping(value="/detailData/{id}",produces="text/html; charset=UTF-8")
	@ResponseBody
	public String detailData(@PathVariable("id") String id,HttpServletRequest request){
		Map data=new HashMap();
		Map record=this.getCommonService().getJdbcDao().queryRow("select * from "+DBTable.config+" where del_flag=0 and id=?",new Object[]{id});	
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		Date date = (Date)record.get("create_time");
		record.put("create_time",sdf.format(date) );
		data.put("data", record);
		
		return WebUtil.resultString(data,request);
	}
	
	//删除用户
	@RequestMapping(value="/delete",produces="text/html; charset=UTF-8")
	@ResponseBody
	public String delete(HttpServletRequest request){

		
		Map data = JSON.parseObject(request.getParameter("data"), Map.class); 
		
		String id=null;
		if(data.get("id")!=null){
			id=data.get("id").toString();
		}
		
		
		if(id==null || id.equals("")){
		}else{	
	        String[] idArray = id.split(",");
	        String tmpVar="?";
	        if(idArray!=null && idArray.length>0){
	        	Matcher matcher = Pattern.compile("\\d+").matcher(id);
	        	tmpVar=matcher.replaceAll("?");
	        }
			//this.getCommonService().getJdbcDao().updateSql("delete from "+DBTable.member+" where id in ("+tmpVar+")",idArray);
	        this.getCommonService().getJdbcDao().updateSql("update "+DBTable.config+" set del_flag=1 where id in ("+tmpVar+")",idArray);
		}
		Map result=new HashMap();
		result.put("result",1);
		result.put("msg", "操作成功");
		
		return WebUtil.resultString(result,request);
	}
	
	/*//更新发布状态
	@RequestMapping(value="/updatePublish",produces="text/html; charset=UTF-8")
	@ResponseBody
	public String updatePublish(HttpServletRequest request){
		String num = "1".equals(request.getParameter("publish"))?"0":"1";
		
		this.getCommonService().getJdbcDao().updateSql("update "+DBTable.confi+" set publish=? where id=?",new Object[]{num,request.getParameter("id")});
		Map result=new HashMap();
		result.put("result",1);
		result.put("msg", "操作成功");
		
		return WebUtil.resultString(result,request);
	}*/
	
	
	
	
}
